# -*- coding: utf-8 -*-
# @Time : 2021/2/7 15:55
# @Author : zhangcheng
# @Software: PyCharm

from models.comment_model import Comment
from init.init import dbsession
from sqlalchemy import func
from utils.commons import query_to_dict


class CommentController(Comment):
    @classmethod
    # 创建留言，需要传入留言者writer、被留言者reader、留言内容content和留言时间time
    def add(cls, **kwargs):
        try:
            # 获得最大的autoid
            max_id = dbsession.query(func.max(cls.autoid)).first()
        except Exception as e:
            return {"message": "数据库异常，生成ID失效", "error": e.args}

        if max_id[0] is None:
            autoid = 1
        else:
            autoid = max_id[0] + 1
        try:
            comment = Comment(autoid=autoid, writer=kwargs.get("writer", ""), reader=kwargs.get("reader"),
                              content=kwargs.get("content"), time=kwargs.get("time"))
            dbsession.add(comment)
            dbsession.commit()
        except Exception as e:
            dbsession.rollback()
            return {"message": "数据库异常，生成留言失效", "error": e.args}
        result = query_to_dict(comment)
        return {"message": "添加成功", "data": result}

    # 根据autoid查询，一次一条,或根据writer或reader查询，一次一个列表
    @classmethod
    def get(cls, **kwargs):
        try:
            # autoid是主键，查询结果只有一条
            if kwargs.get("autoid"):
                comment_info = dbsession.query(Comment).filter_by(autoid=kwargs.get("autoid")).first()
            # 如果传入的是writer则判断是否还有reader传入
            elif kwargs.get("writer"):
                if kwargs.get("reader"):
                    comment_info = dbsession.query(Comment).filter_by(reader=kwargs.get("reader"),
                                                                      writer=kwargs.get("writer")).all()
                else:
                    comment_info = dbsession.query(Comment).filter_by(writer=kwargs.get("writer")).all()
            elif kwargs.get("reader"):
                comment_info = dbsession.query(Comment).filter_by(reader=kwargs.get("reader")).all()
            if not comment_info:
                return {"message": "无留言信息", "error": "no data"}
            result = query_to_dict(comment_info)
        except Exception as e:
            return {"message": "数据库异常，获取数据失败", "error": e.args}
        return {"message": "查询成功", "data": result}

    # 根据autoid删除，一次一条，或根据writer或reader删除，一次一个列表
    @classmethod
    def delete(cls, **kwargs):
        # 先查询判断记录是否存在
        try:
            if kwargs.get("autoid"):
                comment_info = dbsession.query(Comment).filter_by(autoid=kwargs.get("autoid")).first()
            elif kwargs.get("writer"):
                if kwargs.get("reader"):
                    comment_info = dbsession.query(Comment).filter_by(reader=kwargs.get("reader"),
                                                                      writer=kwargs.get("writer")).all()
                else:
                    comment_info = dbsession.query(Comment).filter_by(writer=kwargs.get("writer")).all()
            elif kwargs.get("reader"):
                comment_info = dbsession.query(Comment).filter_by(reader=kwargs.get("reader")).all()
        except Exception as e:
            return {"message": "数据库异常，信息查询失败", "error": e.args}
        if not comment_info:
            return {"message": "无留言信息", "error": "no data"}
        try:
            dbsession.delete(comment_info)
            dbsession.commit()
            result = query_to_dict(comment_info)
            return {"message": "删除成功", "data": result}
        except Exception as e:
            dbsession.rollback()
            return {"message": "数据库异常，删除失败", "error": e.args}

    # 根据autoid更新，一次一条，只能更新content内容
    @classmethod
    def update(cls, **kwargs):
        # 先查询判断记录是否存在
        try:
            comment_info = dbsession.query(Comment).filter_by(autoid=kwargs.get("autoid")).first()
        except Exception as e:
            return {"message": "数据库异常，信息查询失败", "error": e.args}
        if not comment_info:
            return {"message": "无留言信息", "error": "no data"}
        old_data = query_to_dict(comment_info)
        try:
            dbsession.query(Comment).filter_by(autoid=kwargs.get("autoid")).update(kwargs)
            dbsession.commit()
            new_comment_info = dbsession.query(Comment).filter_by(autoid=kwargs.get("autoid")).first()
            new_data = query_to_dict(new_comment_info)
            return {"message": "更新成功", "old_data": old_data, "new_data": new_data}
        except Exception as e:
            dbsession.rollback()
            return {"message": "数据库异常，更新失败", "error": e.args}

